package model;

import entities.ViewPatientMaterial;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.table.AbstractTableModel;

public class EditPatientMaterialModel extends AbstractTableModel {

    private ArrayList<ViewPatientMaterial> list;
    private ArrayList<ViewPatientMaterial> disp;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("MMM, EEE ddd, YYYY");

    public EditPatientMaterialModel() {
        list = new ArrayList<ViewPatientMaterial>();
        disp = new ArrayList<ViewPatientMaterial>();
    }

    public EditPatientMaterialModel(ArrayList<ViewPatientMaterial> list) {
        this.list = new ArrayList<ViewPatientMaterial>(list);
        this.disp = new ArrayList<ViewPatientMaterial>(list);
    }

    @Override
    public int getRowCount() {
        return disp.size();
    }

    @Override
    public int getColumnCount() {
        return 6;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        ViewPatientMaterial record = disp.get(rowIndex);
        Date endDate = record.getEndDate();
        switch (columnIndex) {
            case 0:
                return record.getPatientName();
            case 1:
                return record.getIdentityCard();
            case 2:
                return dateFormat.format(record.getStartDate());
            case 3:
                if(endDate != null) {
                    return dateFormat.format(record.getEndDate());
                }
                return "";
            case 4: 
                return record.getDepartmentName();
            case 5:
                return record.getDoctorName();
            default:
                return null;
        }
    }

    @Override
    public String getColumnName(int column) {
        switch (column) {
            case 0:
                return "Patient";
            case 1:
                return "IdentityCard";
            case 2:
                return "StartDate";
            case 3:
                return "EndDate";
            case 4:
                return "Department";
            case 5:
                return "Doctor";
            default:
                return null;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class;
    }

    public ViewPatientMaterial getAt(int index) {
        return disp.get(index);
    }

    public void addRecord(ViewPatientMaterial record) {
        list.add(record);
        disp.add(record);
        fireTableDataChanged();
    }

    public void setRecord(ViewPatientMaterial record) {
        for (ViewPatientMaterial r : list) {
            if (r.getPatientID() == record.getPatientID()
                    && r.getDoctorID() == record.getDoctorID()
                    && r.getStartDate() == record.getStartDate()) {
                list.set(list.indexOf(r), record);
                int index = disp.indexOf(r);
                if (index != -1) {
                    disp.set(index, record);
                }
            }
        }
    }

    public void removeRecord(ViewPatientMaterial record) {
        list.remove(record);
        disp.remove(record);
        fireTableDataChanged();
    }

    public void filter(String ptid, String did) {
        disp.clear();
        for (ViewPatientMaterial record : list) {
            boolean accept = true;
            if (ptid != null && ptid.length() != 0) {
                int length = ptid.length();
                if (record.getIdentityCard().length() >= length) {
                    if (!record.getIdentityCard().substring(0, length).equalsIgnoreCase(ptid)) {
                        accept = false;
                    }
                } else {
                    accept = false;
                }
            }
            if (did != null && did.length() != 0 && !record.getEmployeeID().equals(did)) {
                accept = false;
            }
            if (accept) {
                disp.add(record);
            }
        }
        fireTableDataChanged();
    }

    @Override
    public void fireTableDataChanged() {
        super.fireTableDataChanged();
    }
}
